<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\supplier\controller;
use think\Controller;
use think\Session;
use app\supplier\model\FieldModel;
use app\supplier\model\CountryModel;
use app\supplier\model\MakeModel;
use app\supplier\model\SeriesModel;
use app\supplier\model\PtypeModel;
use PHPExcel_IOFactory;
use PHPExcel;

class Common extends Controller
{
	/**
	 * 国家区域对象
	 * @return mixed
	 */
	public $country_obj;
	
	/**
	 * 制造商对象
	 * @return mixed
	 */
	public $make_obj;
	
	/**
	 * 系列对象
	 * @return mixed
	 */
	public $series_obj;
	
	/**
	 * 栏目对象
	 * @return mixed
	 */
	public $menu_obj;
	
	/**
	 * 产品类型对象
	 * @return mixed
	 */
	public $ptype_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->country_obj = new CountryModel();	
		$this->make_obj = new MakeModel();
		$this->series_obj = new SeriesModel();
		$this->ptype_obj = new PtypeModel();
	}
	
	/**
	 * 根据领域获取国家
	 * @param $field_id int 领域
	 * @return mixed
	 */
	public function getCountryByField()
	{
		$input = input('post.');
		if ($this->request->isAjax()) {
			$where = array();
			$where['field_id'] = $input['field_id'];
			$ret = $this->country_obj->getCountryRela($where);
			$ret_data = array();
			if(!empty($ret)){
				$country = CountryModel::all();
				$country_arr = array();
				foreach($country as $_c){
					$country_arr[$_c->id] = $_c->country_name;
				}
				foreach($ret as $_r){
					array_push(
							$ret_data,
							array(
								'id'=>$_r['country_id'],
								'country_name'=>$country_arr[$_r['country_id']]
							)
						);
				}
			}
			$result = [
					'code' => 1,
					'msg'  => '成功',
					'list' => $ret_data
				];
		}else{
			$result = [
					'code' => 0,
					'msg'  => '请求不合法',
					'list' => $input
				];
		}
		return json($result);
	}
	
	/**
	 * 根据国家获取制造商
	 * @param $field_id int 领域
	 * @param $country_id int 国家
	 * @return mixed
	 */
	public function getMakeByCountry()
	{
		$input = input('post.');
		if ($this->request->isAjax()) {
			$where = array();
			//$where['field_id'] = $input['field_id'];
			//$where['country_id'] = $input['country_id'];
			$where['f_c_id'] = $input['f_c_id'];
			$where['export'] = 1;
			$ret = $this->make_obj->getMakeList($where);
			$result = [
					'code' => 1,
					'msg'  => '成功',
					'list' => $ret['list']
				];
		}else{
			$result = [
					'code' => 0,
					'msg'  => '请求不合法',
					'list' => $input
				];
		}
		return json($result);
	}
	
	/**
	 * 根据制造商获取系列
	 * @param $field_id int 领域
	 * @param $country_id int 国家
	 * @return mixed
	 */
	public function getSeriesbyMake()
	{
		$input = input('post.');
		if ($this->request->isAjax()) {
			$where = array();
			//$where['field_id'] = $input['field_id'];
			//$where['country_id'] = $input['country_id'];
			$where['make_id'] = $input['make_id'];
			$where['export'] = 1;
			$ret = $this->series_obj->getSeriesList($where);
			$result = [
					'code' => 1,
					'msg'  => '成功',
					'list' => $ret['list']
				];
		}else{
			$result = [
					'code' => 0,
					'msg'  => '请求不合法',
					'list' => $input
				];
		}
		return json($result);
	}
	
	/**
	 * 根据产品类型获取尺码
	 * @param $field_id int 领域
	 * @param $country_id int 国家
	 * @return mixed
	 */
	public function getSizebyType()
	{
		$input = input('post.');
		if ($this->request->isAjax()) {
			$where = array();
			$where['type_id'] = $input['type_id'];
			$size = $this->ptype_obj->getSizeByType($where);
			$ret = array();
			$size_config = config('size_list');
			foreach($size_config as $_k=>$_s){
				if($size['size_'.strtolower($_s['code'])] == 1){
					array_push(
						$ret,
						array('code'=>$_s['code'],'name'=>$_s['name'],'input'=>'size_'.strtolower($_s['code']))
					);
				}
			}
			$result = [
					'code' => 1,
					'msg'  => '成功',
					'list' => $ret
				];
		}else{
			$result = [
					'code' => 0,
					'msg'  => '请求不合法',
					'list' => $input
				];
		}
		return json($result);
	}
	
	/**
	 * 上传附件、图片
	 * @param $file FILE 文件
	 * @return mixed
	 */
	public function upload()
	{
		$post = request()->post();
		$water = isset($post['water']) ? $post['water'] : 0;
		$is_import = isset($post['is_import']) ? $post['is_import'] : 0;
		$upload_dir = config('upload_dir').$post['dir'].'/';
		$show_file = config('show_file_dir').$post['dir'].'/';
		$file = request()->file($post['u_type']);
		$info = $file->move($upload_dir);
		$data = array();
        if($info){
			$arr = $info->getInfo();
			$show_file .= $info->getSaveName();
			if($is_import){
				$extension = strtolower( pathinfo($show_file, PATHINFO_EXTENSION) );
				if($extension == 'xlsx') {
					$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
					$objPHPExcel = $objReader->load($upload_dir.$info->getSaveName(), $encode = 'utf-8');
				}else if($extension == 'xls'){
					$objReader =\PHPExcel_IOFactory::createReader('Excel5');
					$objPHPExcel = $objReader->load($upload_dir.$info->getSaveName(), $encode = 'utf-8');
				}
				$excel_array = $objPHPExcel->getsheet(0)->toArray();
				array_shift($excel_array);
				switch($post['u_type']){
					case 'fact_file' :
						foreach($excel_array as $_arr){
							if(empty($_arr[0])){
								continue;
							}
							array_push(
								$data,
								array(
									'exchange_code'=>$_arr[0],
									'fact_code'=>$_arr[1],
									'fact_name'=>$_arr[2],
									'fact_type'=>$_arr[3],
									'fact_demo'=>$_arr[4],
								)
							);
						}
						break;
				}	
			}
			$result = [
					'code' => 1,
					'msg'  => '上传成功',
					'list' => array('name'=>$arr['name'],'o_url'=>$post['dir'].'/'. $info->getSaveName(),'url'=>$show_file,'data'=>$data)
				];
		}else{
			$result = [
					'code' => 0,
					'msg'  => $file->getError(),
					'list' => array(),
					'data'=>$data
				];
		}
		return json($result);
	}
}
